//
//  ZGLNameInfoController.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/8/23.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//

#import "ZGLNameInfoController.h"
#import "ZGLBankCardInfoController.h"

@interface ZGLNameInfoController ()

@property (weak, nonatomic) IBOutlet UITextField *nameTF;

@property (weak, nonatomic) IBOutlet UITextField *IDNumTF;

@property (weak, nonatomic) IBOutlet UILabel *phoneNumLabel;

@property (weak, nonatomic) IBOutlet UIButton *getVertifiBtn;

@property (weak, nonatomic) IBOutlet UITextField *vertifiTF;






@end

@implementation ZGLNameInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"实名认证";
    NSString *phoneNum = [PublicMethod phoneNumToAsterisk:sZGLUserInfoModel.mobile];
    self.phoneNumLabel.text = phoneNum;
    
}

//获取短信验证码
- (IBAction)getVertifiBtnClick:(UIButton *)sender
{
    [TalkingData trackEvent:ZGLNameAuthenticationBtnEventID label:@"getVerticode"];
    
    BOOL isValidateMobile = false;
    NSString *phoneNum = sZGLUserInfoModel.mobile;
    isValidateMobile = [PublicMethod isValidateMobile:phoneNum];
    if (isValidateMobile) {
        
        //发送请求
        [sZGLHTTPRequestManager getCertificationVerticodeWithSMSType:@"1" Success:^{
            
            [EasyTextView showSuccessText:@"短信已发送，请查收"];
            [sender startCountdownTime:countDownTime withCountDownBlock:nil];//倒计时

        } Fail:^(ZGLRespondStatusCode bef_status, NSString *bef_message, NSError *error) {
            
            [EasyTextView showErrorText:@"短信发送失败，请检查网络"];
        }];
        
    }else{
        
        [EasyTextView showInfoText:@"请输入正确的手机号"];
    }
}

//下一步
- (IBAction)nextBtnClick:(UIButton *)sender
{
    DLog(@"下一步");
    [TalkingData trackEvent:ZGLNameAuthenticationBtnEventID];
    
    NSString *name = _nameTF.text;
    NSString *mobile = sZGLUserInfoModel.mobile;
    NSString *code = _vertifiTF.text;
    NSString *idCard = _IDNumTF.text;
    
    BOOL isNamedCN = [PublicMethod deptNameInputShouldChineseWith:name];
    if (isNamedCN && name.length>=2) {
        
        BOOL isRealIDNum = [PublicMethod checkUserID:_IDNumTF.text];
        if (isRealIDNum) {
            
            NSString *vertifiCode = _vertifiTF.text;
            if (vertifiCode.length>0) {
                //菊花
                [EasyLoadingView showLoadingText:@"" config:^EasyLoadingConfig *{
                    
                    return [EasyLoadingConfig shared].setLoadingType(LoadingShowTypeIndicator);
                }];
                
                DLog(@"发请求验证身份");
                [sZGLHTTPRequestManager RealNameAuthenticationWithMobile:mobile VertiCode:code IdCard:idCard RealName:name Success:^(id responsedata) {
                    
                    NSString *code = responsedata[@"code"];
                    DLog(@"实名认证成功");
                    if ([code integerValue] == 1) {
                        
                        [EasyLoadingView hidenLoading];
                        [EasyTextView showSuccessText:@"认证成功"];
                        
                        ZGLBankCardInfoController *bankCardVc = [[ZGLBankCardInfoController alloc]initWithNibName:@"ZGLBankCardInfoController" bundle:nil];
                        bankCardVc.realNameString = name;
                        [self.navigationController pushViewController:bankCardVc animated:YES];

                    }else if([code integerValue]==0){
                        [EasyTextView showInfoText:@"身份证号码和真实姓名不一致"];
                        [EasyLoadingView hidenLoading];
                    }
                    
                    
                } Fail:^(ZGLRespondStatusCode bef_status, NSString *bef_message, NSError *error) {
                    
                    DLog(@"实名认证失败");
                    [EasyLoadingView hidenLoading];
                    [EasyTextView showInfoText:@"实名认证失败，请重试"];
                }];
                
                
                
            }else{
                
                [EasyTextView showInfoText:@"请输入正确验证码"];
            }
            
        }else{
            
            [EasyTextView showInfoText:@"请输入正确身份证"];
        }
        
    }else{
        
        [EasyTextView showInfoText:@"请输入真名"];
    }
    
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
